package com.example.controller.commodity;

import com.example.pojo.commodity.Product;
import com.example.pojo.commodity.ProductType;
import com.example.service.commodity.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * com.example.controller.commodity
 *
 * @author foam
 * create 2020-12-21
 **/
@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/queryClass")      //类别查询
    public String queryClass(Model model) {
        //总成本
        double cost = 0;
        //总盈亏
        double profit = 0;
        //报废损失
        double scrap_money = 0;
        //毛利润
        double sales_money = 0;
        List<ProductType> ProductTypes = productService.queryAllClass();
        System.out.println(ProductTypes);
        for (ProductType productType : ProductTypes) {
            cost += productType.getProduct_cost();
            profit += productType.getProduct_profit();
            scrap_money += productType.getProduct_scrap();
            sales_money += productType.getProduct_maoprofit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("allProductTypes", ProductTypes);
        model.addAttribute("sales_moneys", sales_money);
        model.addAttribute("costs", cost);
        model.addAttribute("scrap_moneys", scrap_money);
        model.addAttribute("profits", str);

        return "/statistics/statisticsType";
    }

    @RequestMapping("/queryAll")
    public String queryAll(Model model) {
        double profit = 0;
        List<Product> productList = productService.queryAll();
        for (Product product : productList) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", productList);
        return "/statistics/statistics";
    }

    @RequestMapping("/queryAllBySomething")
    public String queryAllBySomething(@RequestParam("proId") String proId,
                                      @RequestParam("proName") String proName,
                                      @RequestParam("proClass") String proClass,
                                      Model model) {
        List<Product> products = productService.queryBySomething(proId, proName, proClass);
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    //    销售量排序
//    @ResponseBody ajax使用
    @RequestMapping("orderBySoldDown")
    public String orderBySoldDown(Model model) {
        List<Product> products = productService.orderBySoldDown();
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    @RequestMapping("orderBySoldUp")
    public String orderBySoldUp(Model model) {
        List<Product> products = productService.orderBySoldUp();
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    //报废量排序
    @RequestMapping("orderByScrapDown")
    public String orderByScrapDown(Model model) {
        List<Product> products = productService.orderByScrapDown();
        double profit = 0;

        for (Product product : products) {
            profit += product.getProduct_profit();

        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    @RequestMapping("orderByScrapUp")
    public String orderByScrapUp(Model model) {
        List<Product> products = productService.orderByScrapUp();
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    //利润额排序
    @RequestMapping("/orderByProfitDown")
    public String orderByProfitDown(Model model) {
        List<Product> products = productService.orderByProfitDown();
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }

    @RequestMapping("/orderByProfitUp")
    public String orderByProitUp(Model model) {
        List<Product> products = productService.orderByProfitUp();
        double profit = 0;
        for (Product product : products) {
            profit += product.getProduct_profit();
        }
        DecimalFormat df = new DecimalFormat("#.0");
        String str = df.format(profit);
        model.addAttribute("profits", str);
        model.addAttribute("allProducts", products);
        return "statistics/statistics";
    }


}
